package com.lantosec.hmrp.modules.morality.controller;


import com.lantosec.hmrp.core.kit.PageVO;
import com.lantosec.hmrp.core.log.ControllerLog;
import com.lantosec.hmrp.core.log.LogType;
import org.springframework.web.bind.annotation.RequestMapping;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.lantosec.hmrp.core.kit.Result;
import com.lantosec.hmrp.modules.morality.entity.MoralityItem;
import com.lantosec.hmrp.modules.morality.service.IMoralityItemService;
import com.lantosec.hmrp.modules.morality.vo.MoralityItemVO;

import java.util.Arrays;
import java.util.List;
import lombok.extern.log4j.Log4j2;
import org.springframework.web.bind.annotation.RestController;
import com.lantosec.hmrp.core.mvc.BaseController;

/**
 *
 * 医德医风项目定义 控制器
 *
 * @author AutoCode
 * @since 2020-04-16
 */
@Api(value = "医德医风项目定义控制器", tags = "医德医风项目定义控制器")
@RestController
@RequestMapping("/morality/item")
@Log4j2
public class MoralityItemController extends BaseController {
    @Autowired
    private IMoralityItemService service;

    @GetMapping("getAll")
    @ApiOperation(value = "查询全部基础项目")
    public Result<List<MoralityItemVO>> getAll(@ApiParam(value = "父类项目ID") @RequestParam(name = "parentItemId",required = false) String parentItemId){
        List<MoralityItemVO> moralityItems = service.getAll(parentItemId);                                                                                                                                                                                           ;
        return Result.ok(moralityItems);
    }

    @GetMapping("getAllBadItem")
    @ApiOperation(value = "查询全部一票认定差项目")
    public Result<List<MoralityItemVO>> getAllBadItem(@ApiParam(value = "父类项目ID") @RequestParam(name = "parentItemId",required = false) String parentItemId){
        List<MoralityItem> moralityItems = service.getAllBadItem(parentItemId);                                                                                                                                                                                           ;
        return Result.ok(entity2VO(moralityItems));
    }

    @GetMapping("all")
    @ApiOperation(value = "查询全部")
    public Result<List<MoralityItemVO>> all(){
        List<MoralityItem> moralityItems = service.list(new QueryWrapper<MoralityItem>().orderByAsc(MoralityItem.ColName.item_order.toString()))                                                                                                                                                                                                  ;
        return Result.ok(entity2VO(moralityItems));
    }

    @GetMapping("page")
    @ApiOperation(value = "分页查询")
    public Result<Page<MoralityItemVO>> page(@ApiParam(value = "分页对象", required = true) PageVO<MoralityItem> page,
                                             @ApiParam(value = "项目名称") @RequestParam(name = "moralityItemName",required = false) String moralityItemName){
        page = service.page(page,new QueryWrapper<MoralityItem>().like(moralityItemName != null,MoralityItem.ColName.morality_item_name.toString(),moralityItemName)
                                                                    .orderByAsc(MoralityItem.ColName.item_order.toString()));
        return Result.ok(entity2VO(page));
    }

    @GetMapping("get/{id}")
    @ApiOperation(value = "主键查询")
    public Result<MoralityItemVO> get(@ApiParam(value = "ID", required = true) @PathVariable(name="id") String id){
        return Result.ok(entity2VO(service.getById(id)));
    }

    @PostMapping("create")
    @ControllerLog(name = "新增医德医风项目定义", type = LogType.add)
    @ApiOperation(value = "新增数据")
    public Result<String> create(@ApiParam(value = "项目定义VO对象", required = true) @RequestBody @Validated MoralityItemVO vo, BindingResult bindingResult){
        validationHandler(bindingResult);
        MoralityItem entity = vo.convertToEntity();
        String moralityItemId = service.queryMaxId(entity.getParentItemId());
        entity.setMoralityItemId(moralityItemId);
        service.save(entity);
        return Result.ok(entity.getMoralityItemId());
    }

    @PutMapping("update")
    @ControllerLog(name = "修改医德医风项目定义", type = LogType.update)
    @ApiOperation(value = "修改数据")
    public Result<Void> update(@ApiParam(value = "项目定义VO对象", required = true) @RequestBody MoralityItemVO vo){
        MoralityItem entity = vo.convertToEntity();
        service.updateById(entity);
        return Result.ok();
    }

    @DeleteMapping(value = "delete/{ids}")
    @ControllerLog(name = "逻辑删除医德医风项目定义", type = LogType.delete)
    @ApiOperation("逻辑删除")
    public Result<Void> delete(@ApiParam(value = "ID列表,用逗号分隔", required = true) @PathVariable(name="ids") String ids){
        if(StringUtils.isNotEmpty(ids)){
            service.removeByIds(Arrays.asList(ids.split(",")));
        }
        return Result.ok();
    }
}
